<template>
  <div class="statistics-wrapper">
    <div class="operate">
      <el-button
        type="primary"
        @click="handleAdd('新增场景', { parkId: props.parkId })"
        ><svg-icon name="add" size="15px" />新增场景</el-button
      >
    </div>
    <div class="scene">
      <div class="scene-item" v-for="(item, index) of tableData" :key="index">
        <div class="scene-item-content">
          <img v-if="item.icon" :src="item.icon" />
          <img
            v-else
            :src="imgConfing[getDictValue('scene_type', item.type)]"
          />
          <div class="scene-item-content-right">
            <span class="scene-item-content-right-title">{{
              getDictValue('scene_type', item.type)
            }}</span>
            <span class="scene-item-content-right-num"
              >设备总数：{{ item.equipmentTotal }} 个</span
            >
            <span class="scene-item-content-right-status">
              <span class="scene-item-content-right-status-online">
                <svg-icon name="success" size="14px" />
                在线：{{ item.onlineTotal }} 个
              </span>
              <span class="scene-item-content-right-status-outline">
                <svg-icon name="error" size="14px" />
                离线：{{ item.offlineTotal }} 个
              </span>
            </span>
            <span class="scene-item-content-right-warning">
              <div class="scene-item-content-right-warning-icon" />
              当前告警事件: {{ item.warning || 0 }} 起
            </span>
          </div>
        </div>
        <div class="scene-item-operate">
          <el-button
            class="scene-item-operate-large"
            link
            type="primary"
            @click="handleGotoDeviceList(item)"
            ><svg-icon name="device-list" size="14px" />设备列表</el-button
          >
          <el-button
            class="scene-item-operate-large line"
            link
            type="primary"
            @click="handleGotoAlarmList(item)"
            ><svg-icon name="warning" size="14px" />告警记录</el-button
          >
          <el-button link type="primary" @click="handleEdit(item, '编辑场景')"
            >编辑</el-button
          >
          <el-button link type="danger" @click="handleDelete(item)"
            >删除</el-button
          >
        </div>
      </div>
    </div>
    <Add ref="addRef" :detail="currentDetail" @close="handleClose" />
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import monitor from '@/assets/imgs/manager/monitor.png'
import fence from '@/assets/imgs/manager/fence.png'
import person from '@/assets/imgs/manager/person.png'
import gateway from '@/assets/imgs/manager/gateway.png'
import house from '@/assets/imgs/manager/house.png'
import slope from '@/assets/imgs/manager/slope.png'
import Add from './components/sceneAdd.vue'
import {
  getSceneList,
  sceneDelete,
} from '@/api/manager/dataManage/deviceManage.js'
import { useDict } from '@/hooks/useDict'
const { dict, getDictValue } = useDict([])

const props = defineProps({
  parkId: {
    type: String,
  },
})
const searchParams = ref({})
const imgConfing = ref({
  视频监控: monitor,
  电子围栏: fence,
  人员门禁: person,
  无人道闸: gateway,
  危房监测: house,
  边坡监测: slope,
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  // total,
  loading,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getSceneList, sceneDelete)

const emit = defineEmits(['gotoList'])
const handleGotoDeviceList = (item) => {
  emit('gotoList', 'device', item.name)
}
const handleGotoAlarmList = (item) => {
  emit('gotoList', 'alarm', item.name)
}

watch(
  () => props.parkId,
  () => {
    searchParams.value.parkId = props.parkId
    initTableList(1)
  },
)
onMounted(() => {
  paginationParams.value.pageSize = 9999
  initTableList(1)
})
</script>
<style lang="scss" scoped>
.statistics-wrapper {
  margin-top: 20px;
  width: calc(100% - 20px);
  height: 100%;
  background-color: #ffffff;
  box-sizing: border-box;
  padding: 20px 30px;
  .operate {
    width: 100%;
    margin-bottom: 20px;
  }
  .scene {
    width: 100%;
    height: fit-content;
    max-height: calc(100% - 50px);
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, 425px);
    grid-gap: 20px;
    &-item {
      width: 425px;
      height: 236px;
      background: #f8fbff;
      box-shadow: 0px 0px 5px 0px rgba(120, 134, 151, 0.2);
      &-content {
        width: 100%;
        height: calc(100% - 48px);
        box-sizing: border-box;
        padding: 18px 24px;
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        img {
          width: 120px;
          height: 100%;
        }
        &-right {
          height: 100%;
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          justify-content: space-between;
          box-sizing: border-box;
          .svg-icon {
            margin-right: 5px;
          }
          &-title {
            font-size: 20px;
            font-weight: bold;
            color: #333333;
          }
          &-num {
            font-size: 14px;
            font-weight: 400;
            color: #757575;
          }
          &-status {
            display: flex;
            &-online {
              min-width: 110px;
              padding: 0 5px;
              height: 26px;
              background: #ffffff;
              border-radius: 4px;
              display: flex;
              align-items: center;
              justify-content: center;
              font-size: 14px;
              font-weight: 400;
              color: #07c160;
              margin-right: 12px;
            }
            &-outline {
              min-width: 110px;
              padding: 0 5px;
              height: 26px;
              background: #ffffff;
              border-radius: 4px;
              display: flex;
              align-items: center;
              justify-content: center;
              font-size: 14px;
              font-weight: 400;
              color: #9a9a9a;
            }
          }
          &-warning {
            &-icon {
              width: 28px;
              height: 32px;
              background: url('@/assets/imgs/manager/jg.png') no-repeat;
              background-size: 100% 100%;
            }
            font-size: 14px;
            font-weight: 400;
            color: #757575;
            display: flex;
            align-items: center;
          }
        }
      }
      &-operate {
        width: 100%;
        height: 48px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-top: 1px solid #d5deed;
        box-sizing: border-box;
        padding: 0 26px;
        &-large {
          margin-right: 30px;
          position: relative;
        }
        .line {
          &::before {
            content: ' ';
            position: absolute;
            top: 5px;
            right: -35px;
            width: 1px;
            height: 20px;
            background: #d5deed;
            border-radius: 1px;
          }
        }
      }
    }
  }
}
</style>
